<template>
  <div>
    <div class="panel" bar>
      <h2>全国大城市网格覆盖率</h2>
      <div class="chart">
        <div id="gauge" style="width: 100%; height: 100%"></div>
      </div>
      <div class="panel-header"></div>
      <div class="panel-footer"></div>
    </div>
    <div class="panel" pie>
      <h2>空气质量指数级别分布</h2>
      <div class="chart">
        <div id="pie" style="width: 100%; height: 90%"></div>
      </div>
      <div class="panel-header"></div>
      <div class="panel-footer"></div>
    </div>
    <div class="panel" line>
      <h2>12个月内空气质量超标趋势</h2>
      <div class="chart">
        <div id="line" style="width: 100%; height: 100%"></div>
      </div>
      <div class="panel-header"></div>
      <div class="panel-footer"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import requset from '@/utils/requset'

export default {
  mounted() {
    this.initPie(), this.initLine(), this.initGauge()
  },
  methods: {
    initPie() {
      let chartDom = document.getElementById('pie')
      let myChart = echarts.init(chartDom)
      let option
      let result
      requset.get('/1').then((res) => {
        if (res.code === 200) {
          //开始渲染数据
          result = res.data
        } else {
          alert('Pie数据错误')
        }
      })
      option = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '质量',
            type: 'pie',
            radius: '50%',
            data: result,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }

      option && myChart.setOption(option)
    },
    initLine() {
      let chartDom = document.getElementById('line')
      let myChart = echarts.init(chartDom)
      let option
      let result
      requset.get('/1').then((res) => {
        if (res.code === 200) {
          //开始渲染数据
          result = res.data
        } else {
          alert('Pie数据错误')
        }
      })
      option = {
        xAxis: {
          type: 'category',
          data: result.name
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: result.value,
            type: 'line',
            smooth: true
          }
        ]
      }

      option && myChart.setOption(option)
    },
    initGauge() {
      let chartDom = document.getElementById('gauge')
      let myChart = echarts.init(chartDom)
      let option
      let result
      requset.get('/1').then((res) => {
        if (res.code === 200) {
          //开始渲染数据
          result = res.data
        } else {
          alert('Pie数据错误')
        }
      })
      option = {
        tooltip: {
          formatter: '{a} ：<br/>{c}%'
        },
        series: [
          {
            name: '网格覆盖率',
            type: 'gauge',
            progress: {
              show: true
            },
            detail: {
              valueAnimation: true,
              formatter: '{value}'
            },
            data: [result]
          }
        ]
      }

      option && myChart.setOption(option)
    }
  }
}
</script>

<style scoped>
.panel {
  position: relative;
  height: 310px;
  border: 1px solid rgba(25, 186, 139, 0.17);
  background-image: url('@/assets/images/line.png');
  padding: 0 15px 40px;
  margin-bottom: 15px;
}
.panel::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.04);
  pointer-events: none;
}
h2 {
  height: 48px;
  line-height: 48px;
  text-align: center;
  color: #fff;
  font-size: 20px;
  font-weight: 400;
}
.chart {
  height: 240px;
}
.panel-header {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}
.panel-header::before {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 10px;
  height: 10px;
  border-left: 2px solid #02a6b5;
  border-top: 2px solid #02a6b5;
}
.panel-header::after {
  position: absolute;
  content: '';
  top: 0;
  right: 0;
  width: 10px;
  height: 10px;
  border-right: 2px solid #02a6b5;
  border-top: 2px solid #02a6b5;
}
.panel-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
}
.panel-footer::before {
  position: absolute;
  content: '';
  bottom: 0;
  left: 0;
  width: 10px;
  height: 10px;
  border-left: 2px solid #02a6b5;
  border-bottom: 2px solid #02a6b5;
}
.panel-footer::after {
  position: absolute;
  content: '';
  bottom: 0;
  right: 0;
  width: 10px;
  height: 10px;
  border-right: 2px solid #02a6b5;
  border-bottom: 2px solid #02a6b5;
}
</style>
